using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Holoville.HOTween;
using UnityEngine;
using UnityEngine.SceneManagement;

public class TutorialManager : MonoBehaviour
{
	[SerializeField]
	private List<GameObject> listAnimation = new List<GameObject>();

	public static TutorialManager instance;

	public static string reputationTutorialStarted = "ReputationTutorialIsStarted";

	public static string weaponKey = "Tutorial_Famas";

	public static string armorKey = "Tutorial_Armor";

	public bool enableTutorial;

	public int stepTutorial;

	public bool tutorialIsStart;

	public SectorMobManager sectorMob;

	[HideInInspector]
	public UILabel lbTextTutorial;

	[HideInInspector]
	public UILabel lbTextTutorialTony;

	[HideInInspector]
	public UISprite sprEndTutor;

	[HideInInspector]
	public GameObject parentAllTutButton;

	public GameObject ukazatel;

	public GameObject car;

	public GameObject carPointCollider;

	public GameObject carArrows;

	public GameObject animBtnTap;

	public GameObject animBtnSwipeLR;

	public GameObject animBtnSwipeFB;

	public GameObject animBtnSwipeLeft;

	public GameObject animBtnSwipeRight;

	public GameObject animBtnRotate;

	public GameObject animBtnTapShoot;

	public GameObject animCarGyroRighr;

	public GameObject animCarGyroLeft;

	public ButtonMover buttonMover;

	public GameObject interfaceCamera;

	public Transform parentForAnimation;

	public List<TutorialButton> listTutButton = new List<TutorialButton>();

	public List<TutorialBase> listTutorial = new List<TutorialBase>();

	public List<float> listStepWait = new List<float>();

	public ScriptHuman tony;

	public Transform tonyWaitPoint;

	[HideInInspector]
	public string stepTutorialForFlurry = "Start Tutor";

	public GameObject CarControls;

	public bool carControlsSelected;

	public GameObject windowFreeWeapon;

	public UIButton buttonFreeWeapon;

	[SerializeField]
	private SpawnPointChief.SpawnPoint startPlayerPosition;

	[SerializeField]
	private Vector3 tonySpawPosition;

	private UIPanel controlsPanel;

	public void ShowCarControls()
	{
		CarControls.SetActive(true);
		controlsPanel = CarControls.GetComponent<UIPanel>();
		controlsPanel.alpha = 0f;
		HOTween.Init();
		HOTween.Kill(controlsPanel);
		HOTween.To(controlsPanel, 0.5f, new TweenParms().Prop("alpha", 1));
		CarControls.SetActive(true);
		carControlsSelected = false;
	}

	public void HideCarControls()
	{
		CarControls.SetActive(true);
		controlsPanel = CarControls.GetComponent<UIPanel>();
		HOTween.Init();
		HOTween.Kill(controlsPanel);
		HOTween.To(controlsPanel, 0.25f, new TweenParms().Prop("alpha", 0));
		StartCoroutine(DisableCarControls());
		carControlsSelected = true;
		carArrows.SetActive(true);
		carPointCollider.SetActive(false);
	}

	private IEnumerator DisableCarControls()
	{
		yield return new WaitForSeconds(0.3f);
		CarControls.SetActive(false);
	}

	private void Awake()
	{
		BCWStorage.Set(settings.keyCryptoTutorialActive, false);
		Save.SaveBool(settings.keyInfoAfterTutorial, true);
		if (!enableTutorial)
		{
			base.gameObject.SetActive(false);
			return;
		}
		instance = this;
		settings.learningCompleted = false;
		settings.updateOfflineMode(true);
		QualityController.InstIfNeed();
		carArrows.SetActive(false);
	}

	private void Start()
	{
		lbTextTutorial = GameTopWindow.instance.lbTextTutorial;
		lbTextTutorialTony = GameTopWindow.instance.lbTextTutorialTony;
		sprEndTutor = GameTopWindow.instance.sprEndTutor;
		sprEndTutor.gameObject.SetActive(true);
		parentAllTutButton = WindowManager.instance.gameObject;
		TutorialButton[] componentsInChildren = parentAllTutButton.GetComponentsInChildren<TutorialButton>(true);
		BCWDebug.Log("arrTutButton.Length = " + componentsInChildren.Length);
		listTutButton.AddRange(componentsInChildren);
		for (int i = 0; i < listTutorial.Count; i++)
		{
			if (listTutorial[i] != null)
			{
				TutorialBase component = Object.Instantiate(listTutorial[i].gameObject).GetComponent<TutorialBase>();
				component.transform.parent = base.transform;
				listTutorial[i] = component;
				component.gameObject.SetActive(false);
			}
		}
		GameObject gameObject = NGUITools.AddChild(interfaceCamera, buttonMover.gameObject);
		gameObject.transform.localPosition = Vector3.zero;
		buttonMover = gameObject.GetComponent<ButtonMover>();
		gameObject.SetActive(false);
		ActivityIndicator.activEnabled = false;
		tutorialIsStart = true;
		settings.typeControlerCar = TypeControlCar.button;
		StartCoroutine(StartStepTutorial());
		MessageWindow.instance.SetVisiblePanelLoading(false, string.Empty);
		WindowManager.instance.HideAllWindow();
		SetActiveButtonId(TutorialButtonId.other, false);
		GetTutorialButtonById(TutorialButtonId.sitInCar).gameObject.SetActive(false);
	}

	private void OnApplicationPause(bool pauseStatus)
	{
		if (pauseStatus)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("Otval new", stepTutorialForFlurry);
			dictionary.Add("Percent passed", "Skipped");
			FlurryWrapper.LogEvent(FlurryWrapper.EV_TUTORIAL_CLOSE, dictionary);
		}
	}

	private void OnDestroy()
	{
		settings.CathedExeptions += 5;
		settings.SaveCathedExeptionsToStorager();
		instance = null;
	}

	private void Update()
	{
		if (tutorialIsStart && stepTutorial < listTutorial.Count && listTutorial[stepTutorial] != null && listTutorial[stepTutorial].isStarted)
		{
			listTutorial[stepTutorial].OnUpdate();
		}
	}

	public void StartNextStep()
	{
		if (stepTutorial < listTutorial.Count)
		{
			if (listTutorial[stepTutorial] != null && listTutorial[stepTutorial].isStarted)
			{
				listTutorial[stepTutorial].OnUpdate();
			}
			else
			{
				CompleteCurStepTutorial();
			}
		}
	}

	private IEnumerator StartStepTutorial()
	{
		foreach (TutorialBase curTut in listTutorial)
		{
			if (curTut != null && curTut.isStarted)
			{
				curTut.OnStop();
			}
		}
		if (stepTutorial < listTutorial.Count && listTutorial[stepTutorial] != null)
		{
			BCWDebug.Log("StartStepTutorial =" + stepTutorial);
			StopAnimAllButton();
			float second = ((stepTutorial >= listStepWait.Count) ? 0f : listStepWait[stepTutorial]);
			yield return new WaitForSeconds(second);
			if (listTutorial[stepTutorial].needActiveButtonOnStart)
			{
				ShowButtonForCurrentStep();
			}
			listTutorial[stepTutorial].OnStart();
		}
		else
		{
			CompleteCurStepTutorial();
		}
		yield return null;
	}

	public void ShowButtonForCurrentStep()
	{
		ShowButtonForTut(listTutorial[stepTutorial]);
	}

	public void CompleteCurStepTutorial()
	{
		BCWDebug.Log("CompleteCurStepTutorial");
		stepTutorial++;
		if (stepTutorial >= listTutorial.Count)
		{
			EndTutorial();
		}
		else
		{
			StartCoroutine(StartStepTutorial());
		}
	}

	public void EndTutorial()
	{
		StopAnimAllButton();
		BCWStorage.Set(settings.keyCryptoTutorialActive, true);
		Save.SaveBool(settings.keyInfoAfterTutorial, false);
		Save.SaveBool(settings.keyShowLevelAfterTutorial, true);
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("Percent passed", "Passed");
		FlurryWrapper.LogEvent(FlurryWrapper.EV_TUTORIAL_CLOSE, dictionary);
		FlurryWrapper.LogEvent(FlurryWrapper.EV_NEW_PLAYERS);
		BCWDebug.Log("EndTutorial");
		if (FyberWrapper.instance != null)
		{
			FyberWrapper.instance.leaveLocation = ADSLocation.LeaveTutorial;
		}
		settings.addConsumable(10, "grenade");
		settings.CathedExeptions += 34995;
		settings.CathedWarnings += 250;
		PlayerPrefs.Save();
		BCWStorage.SaveAll();
		tutorialIsStart = false;
		SceneManager.LoadSceneAsync("Lobby");
	}

	public void PlayAnimButton(TutorialButtonId curId, GameObject needAnim = null)
	{
		BCWDebug.Log("PlayAnimButton = " + curId);
		StopAnimAllButton();
		foreach (TutorialButton item in listTutButton)
		{
			if (item.id == curId)
			{
				if (needAnim == null)
				{
					item.StartAnimation();
				}
				else
				{
					item.StartAnimation(needAnim);
				}
				break;
			}
		}
	}

	public void StopAnimAllButton()
	{
		foreach (TutorialButton item in listTutButton)
		{
			if (item.isPlayAnim)
			{
				item.StopAnimation();
			}
		}
	}

	public void ShowButtonForTut(TutorialBase curTut)
	{
		foreach (TutorialButton item in listTutButton)
		{
			if (curTut.arrActiveButton.Contains(item.id))
			{
				item.SetActive(true);
			}
			else if (item.id != TutorialButtonId.shoot)
			{
				item.SetActive(false);
			}
		}
	}

	public void SetActiveButtonId(TutorialButtonId curId, bool isActive)
	{
		foreach (TutorialButton item in listTutButton)
		{
			if (item.id == curId)
			{
				item.SetActive(isActive);
			}
		}
	}

	public TutorialButton GetTutorialButtonById(TutorialButtonId curId)
	{
		foreach (TutorialButton item in listTutButton)
		{
			if (item.id == curId)
			{
				return item;
			}
		}
		return null;
	}

	public GameObject AddUkazatelOn(GameObject needObj)
	{
		GameObject gameObject = null;
		if (needObj != null)
		{
			gameObject = Object.Instantiate(ukazatel);
			gameObject.transform.parent = needObj.transform;
			gameObject.transform.localPosition = Vector3.zero;
			gameObject.transform.localRotation = Quaternion.identity;
		}
		return gameObject;
	}

	public Vector3 GetPoint()
	{
		int num = Random.Range(0, sectorMob.sectorPoints.Length);
		Vector3[] sectorPoints = sectorMob.sectorPoints;
		Vector3 result = sectorPoints[0];
		Vector3[] sectorPoints2 = sectorMob.sectorPoints;
		for (int i = 0; i < sectorPoints2.Length; i++)
		{
			Vector3 vector = sectorPoints2[i];
			if (vector.z < result.z)
			{
				result = vector;
			}
		}
		Vector3[] sectorPoints3 = sectorMob.sectorPoints;
		for (int j = 0; j < sectorPoints3.Length; j++)
		{
			Vector3 vector2 = sectorPoints3[j];
			if (vector2.z < GameController.instance.playerScript.transform.position.z && vector2.z > result.z)
			{
				result = vector2;
			}
		}
		return result;
	}

	public void Skip()
	{
		EndTutorial();
	}

	public SpawnPointChief.SpawnPoint GetSpawnPoint()
	{
		return startPlayerPosition;
	}

	public static bool TuturialWorking()
	{
		return (instance != null && instance.tutorialIsStart) || SceneManagerHelper.ActiveSceneName == "Tutorial";
	}

	public void StartLoadNextComics()
	{
		TutorialBase tutorialBase = listTutorial.Where((TutorialBase x) => x != null && x.GetType() == typeof(TutorialStepComics) && !(x as TutorialStepComics).pictureIsLoading).FirstOrDefault();
		if ((bool)tutorialBase)
		{
			StartCoroutine(LoadComicsPrefab(tutorialBase as TutorialStepComics));
		}
	}

	private IEnumerator LoadComicsPrefab(TutorialStepComics step)
	{
		step.pictureIsLoading = true;
		ResourceRequest pict = Resources.LoadAsync(TutorialStepComics.picturesPath + step.pictureName);
		yield return pict;
		step.picture = Object.Instantiate(pict.asset, Vector3.zero, Quaternion.identity) as GameObject;
		step.picture.transform.SetParent(CarControls.transform.parent);
		step.picture.transform.localScale = Vector3.one;
		step.picture.SetActive(false);
	}

	public GameObject GetAnimation(string name)
	{
		foreach (GameObject item in listAnimation)
		{
			if (item.name == name)
			{
				return item;
			}
		}
		return null;
	}

	public void ReturnToList(GameObject go)
	{
		go.SetActive(false);
		go.transform.SetParent(parentForAnimation);
	}

	private void OnDrawGizmos()
	{
		Gizmos.color = Color.green;
		Gizmos.DrawCube(startPlayerPosition.position + Vector3.up, new Vector3(0.5f, 2f, 0.5f));
		Gizmos.color = Color.black;
		Gizmos.DrawCube(tonySpawPosition + Vector3.up * 0.9f, new Vector3(0.5f, 2f, 0.5f));
	}

	private void OnGUI()
	{
	}
}
